package com.ruoyi.web.admin.controller.system;

import java.util.List;

import com.ruoyi.system.domain.SysAlert;
import com.ruoyi.system.domain.qo.AlertListQo;
import com.ruoyi.system.domain.vo.AlertListVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysAlertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiParam;

/**
 * 预警列表操作处理
 *
 * @author ruoyi
 */
@Api(tags = "预警列表管理")
@RestController
@RequestMapping("/system/alert")
public class SysAlertController extends BaseController {

    private final ISysAlertService sysAlertService;

    public SysAlertController(ISysAlertService sysAlertService) {
        this.sysAlertService = sysAlertService;
    }

    /**
     * 获取预警列表
     */
    @ApiOperation(value = "获取预警列表", notes = "获取所有预警信息")
    @PreAuthorize("@ss.hasPermi('system:alert:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAlert sysAlert) {
        startPage();
        List<SysAlert> list = sysAlertService.selectSysAlertList(sysAlert);
        return getDataTable(list);
    }

    /**
     * 获取预警列表分页，含用户名
     */
    @ApiOperation(value = "获取预警列表分页，含用户名", notes = "获取预警列表分页，含用户名")
    @PreAuthorize("@ss.hasPermi('system:alert:list')")
    @GetMapping("/page")
    public TableDataInfo page(AlertListQo alertListQo) {
        startPage();
        List<AlertListVo> list = sysAlertService.selectAlertList(alertListQo);
        return getDataTable(list);
    }

    /**
     * 获取预警聚合分页
     */
    @ApiOperation(value = "获取预警聚合分页，含用户名", notes = "获取预警聚合分页")
    @PreAuthorize("@ss.hasPermi('system:alert:list')")
    @GetMapping("/aggregate")
    public TableDataInfo selectAggregateList(AlertListQo alertListQo) {
        startPage();
        List<AlertListVo> list = sysAlertService.selectAggregateList(alertListQo);
        return getDataTable(list);
    }

    /**
     * 根据预警编号获取详细信息
     */
    @ApiOperation(value = "获取预警详细信息", notes = "根据id获取预警详细信息")
    @ApiImplicitParam(name = "id", value = "预警ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('system:alert:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(sysAlertService.selectSysAlertById(id));
    }

    /**
     * 新增预警
     */
    @ApiOperation(value = "新增预警", notes = "新增预警信息")
    @PreAuthorize("@ss.hasPermi('system:alert:add')")
    @Log(title = "预警列表管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysAlert sysAlert) {
        return toAjax(sysAlertService.insertSysAlert(sysAlert));
    }

    /**
     * 修改预警
     */
    @ApiOperation(value = "修改预警", notes = "修改预警信息")
    @PreAuthorize("@ss.hasPermi('system:alert:edit')")
    @Log(title = "预警列表管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysAlert sysAlert) {
        return toAjax(sysAlertService.updateSysAlert(sysAlert));
    }

    /**
     * 删除预警
     */
    @ApiOperation(value = "批量删除预警", notes = "批量删除预警信息")
    @ApiImplicitParam(name = "ids", value = "预警ID数组", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long[].class)
    @PreAuthorize("@ss.hasPermi('system:alert:remove')")
    @Log(title = "预警列表管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysAlertService.deleteSysAlertByIds(ids));
    }

    /**
     * 删除单个预警
     */
    @ApiOperation(value = "删除单个预警", notes = "删除单个预警信息")
    @ApiImplicitParam(name = "id", value = "预警ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('system:alert:remove')")
    @Log(title = "预警列表管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(sysAlertService.deleteSysAlertById(id));
    }

    @ApiOperation("查询用户最近一个预警信息")
    @GetMapping("/latest/user/{userId}")
    public AjaxResult getLatestAlertByUserId(@ApiParam("用户ID") @PathVariable("userId") Long userId) {
        return success(sysAlertService.selectLatestAlertByUserId(userId));
    }
}